require('../../assets/css/index.less')
require('../../assets/fonts/iconfont.css')

// 当前页面样式
require('./home.less')
var Swiper = require('../../assets/lib/swiper/swiper.css')
var Swiper = require('../../assets/lib/swiper/swiper.js')
let https = require('../../utils/https.js');
let dom = require('../../utils/dom.js')
dom.isLogin()
// 设置轮播 
dom.ready(function () {
    //调用底部菜单
    dom.bMenu('home')

    var mySwiper = new Swiper('.swiper', {
        loop: true, // 循环模式选项

        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },

        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            disableOnInteraction: false, //用户操作之后禁用
        }
    })
    function getUserInfo() {
        https.get('/api/user/info', function (res) {

            let { errno, data } = res;
            if (errno == 0) {
                //渲染数据到网页
                dom.getEle('#rankingVal').innerHTML = data.ranking
                dom.getEle('#clockVal').innerHTML = `累计打卡${data.clockCount}天`
                dom.getEle('#badgesVal').innerHTML = data.badges
            }
        })
    }

    //当网页加载时候获取数据
    getUserInfo()
    function getClockIn() {
        https.get('/api/user/clockInInfo', function (res) {
            let { errno, data } = res;
            if (errno == 0) {
                //渲染数据
                dom.getEle('#clockbtn').innerHTML = data.isClockIn ? '已经打卡' : '今日打卡'
            }
        })
    }
    getClockIn()
    dom.addEvent('#clockbtn', function () {
        https.post('/api/user/clockIn', {}, function (res) {
            console.log(res);
            //解构赋值
            //判定
            if (res.errno == -1) {
                //  alert('你已经打过卡啦，明天再来吧！')
                weui.alert('你已经打过卡啦，明天再来吧！')
            } else {
                alert('恭喜你，打卡成功')
            }
        })
    })
})